﻿using RotovapPlatform.UI.ViewModels.MaterialManagement;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static RotovapPlatform.Domain.Business.Machine.Enums.ExperimentConfigEnums;
using System.Windows.Controls;
using System.Windows;
using RotovapPlatform.Domain.Business.Storage.Enums;

namespace RotovapPlatform.UI.Selectors
{
	/// <summary>
	/// 瓶子模板选择器
	/// 根据瓶子类型和状态选择不同的模板
	/// </summary>
	public class BottleTemplateSelector : DataTemplateSelector
	{
		// 空槽位模板
		public DataTemplate EmptySlotTemplate { get; set; }

		// 基本瓶子模板
		public DataTemplate CentrifugeBottleTemplate { get; set; }
		public DataTemplate RotovapBottle500mlTemplate { get; set; }
		public DataTemplate RotovapBottle1000mlTemplate { get; set; }
		public DataTemplate SampleBottleTemplate { get; set; }

		// 离心瓶模板
		public DataTemplate CentrifugeBottleEmptyTemplate { get; set; }
		public DataTemplate CentrifugeBottleCleanTemplate { get; set; }
		public DataTemplate CentrifugeBottleOriginalTemplate { get; set; }
		public DataTemplate CentrifugeBottleCollectedLiquidTemplate { get; set; }
		public DataTemplate CentrifugeBottleResidueTemplate { get; set; }

		// 旋蒸瓶500ml模板
		public DataTemplate RotovapBottle500mlEmptyTemplate { get; set; }
		public DataTemplate RotovapBottle500mlResidueTemplate { get; set; }

		// 旋蒸瓶1000ml模板
		public DataTemplate RotovapBottle1000mlEmptyTemplate { get; set; }
		public DataTemplate RotovapBottle1000mlResidueTemplate { get; set; }

		// 取样瓶模板
		public DataTemplate SampleBottleEmptyTemplate { get; set; }
		public DataTemplate SampleBottleCollectedTemplate { get; set; }
		public DataTemplate SampleBottleResidueTemplate { get; set; }

		public override DataTemplate SelectTemplate(object item, DependencyObject container)
		{
			if (item is ShelfSlotViewModel slot)
			{
				// 如果槽位没有瓶子，返回空槽位模板
				if (!slot.IsOccupied || slot.Bottle == null)
				{
					return EmptySlotTemplate;
				}

				// 根据瓶子类型和状态选择模板
				if (slot.Bottle is CentrifugeBottle)
				{
					// 离心瓶
					var function = slot.Bottle.Function;

					switch (function)
					{
						case FunctionType.Centrifuge_Empty:
							return CentrifugeBottleEmptyTemplate;
						case FunctionType.Centrifuge_Wash_Liquid:
							return CentrifugeBottleCleanTemplate;
						case FunctionType.Centrifuge_To_Rotovap:
							return CentrifugeBottleOriginalTemplate;
						case FunctionType.Centrifuge_Condensate_Collection:
							return CentrifugeBottleCollectedLiquidTemplate;
						case FunctionType.Centrifuge_Residue_Collection:
							return CentrifugeBottleResidueTemplate;
						default:
							return CentrifugeBottleTemplate;
					}
				}
				else if (slot.Bottle is RotovapBottle rotovapBottle)
				{
					// 旋蒸瓶
					var function = rotovapBottle.Function;
					bool is1000ml = rotovapBottle.Type == ItemType.Rotovap_1000ml;

					if (is1000ml)
					{
						// 1000ml旋蒸瓶
						switch (function)
						{
							case FunctionType.Rotovap_Empty:
								return RotovapBottle1000mlEmptyTemplate;
							case FunctionType.Rotovap_NotEmpty:
								return RotovapBottle1000mlResidueTemplate;
							default:
								return RotovapBottle1000mlTemplate;
						}
					}
					else
					{
						// 500ml旋蒸瓶
						switch (function)
						{
							case FunctionType.Rotovap_Empty:
								return RotovapBottle500mlEmptyTemplate;
							case FunctionType.Rotovap_NotEmpty:
								return RotovapBottle500mlResidueTemplate;
							default:
								return RotovapBottle500mlTemplate;
						}
					}
				}
				else if (slot.Bottle is SampleBottle)
				{
					// 取样瓶
					var function = slot.Bottle.Function;

					switch (function)
					{
						case FunctionType.Sample_Empty:
							return SampleBottleEmptyTemplate;
						case FunctionType.Sample_Condensate:
							return SampleBottleCollectedTemplate;
						case FunctionType.Sample_Residue:
							return SampleBottleResidueTemplate;
						default:
							return SampleBottleTemplate;
					}
				}
			}

			// 默认返回空槽位模板
			return EmptySlotTemplate;
		}
	}
}
